package com.example.tanlee.ocrgraduation.activity;

import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.tanlee.ocrgraduation.R;
import com.example.tanlee.ocrgraduation.application.MyApplication;
import com.example.tanlee.ocrgraduation.util.ImageLoaderUtil;
import com.example.tanlee.ocrgraduation.util.StringConstant;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * write by tanlee 2017.2.21
 * 用户详情处理页面
 */
public class ProcessDetailActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageView photoImg;
    private TextView processState; //处理状态
    private TextView processDateTxt;
    private TextView processTimeTxt;
    private TextView processResultTxt;
    private TextView processRateTxt;
    private ImageView returnBtn; // 返回按钮
    private TextView processBtn; // 处理按钮
    private SweetAlertDialog sweetAlertDialog; // 弹出的对话窗口
    private int tempCount = 0;

    private String targetImgString;  // 目标图片地址

    private void initData(){
        targetImgString = getIntent().getStringExtra(StringConstant.INTENT_IMG_STRING);
        if (targetImgString == null) {
            return;
        }
        else if(targetImgString.equals("")){
            return;
        }
        else{
            MyApplication.getImageLoader().displayImage(
                    targetImgString,
                    photoImg,
                    ImageLoaderUtil.getRoundImageOptions(0),
                    new SimpleImageLoadingListener());
        }
    }

    private void setListener(){
        returnBtn.setOnClickListener(this);
        processBtn.setOnClickListener(this);
    }

    private void initView(){
        photoImg = (ImageView) findViewById(R.id.img_process_picture);
        processState = (TextView) findViewById(R.id.txt_detail_process_state);
        processDateTxt = (TextView) findViewById(R.id.txt_process_detail_date);
        processTimeTxt = (TextView) findViewById(R.id.txt_process_detail_picture_time);
        processRateTxt = (TextView) findViewById(R.id.txt_detail_success_rate);
        processResultTxt = (TextView) findViewById(R.id.txt_process_detail_info);
        returnBtn = (ImageView) findViewById(R.id.user_info_back_btn);
        processBtn = (TextView) findViewById(R.id.txt_process_detail_btn);
    }

    private void showDialog(){
        final SweetAlertDialog pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE)
                .setTitleText("Loading");
        pDialog.show();
        pDialog.setCancelable(false);
        new CountDownTimer(800 * 5, 800) {
            public void onTick(long millisUntilFinished) {
                // you can change the progress bar color by ProgressHelper every 800 millis
                tempCount ++;
                switch (tempCount){
                    case 0:
                        pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.blue_btn_bg_color));
                        break;
                    case 1:
                        pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_50));
                        break;
                    case 2:
                        pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color));
                        break;
                    case 3:
                        pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_20));
                        break;
                    case 4:
                        pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_blue_grey_80));
                        break;
//                    case 5:
//                        pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.warning_stroke_color));
//                        break;
//                    case 6:
//                        pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color));
//                        break;
                }
            }

            public void onFinish() {
                tempCount = -1;
                pDialog.setTitleText("Success!")
                        .setConfirmText("OK")
                        .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
                MyApplication.getInstance().count ++;
                if(MyApplication.getInstance().count / 2 == 0){
                    processTimeTxt.setText(StringConstant.PROCESS_TIME + "5.1s");
                    processResultTxt.setText(StringConstant.RESULT_STRING1);
                }
                else {
                    processResultTxt.setText(StringConstant.RESULT_STRING2);
                    processTimeTxt.setText(StringConstant.PROCESS_TIME + "4.3s");
                }
                processState.setText(StringConstant.PROCESS_STATUS + "已处理");
            }
        }.start();
    }


    private void initDialog(){
        sweetAlertDialog = new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
                .setTitleText("提醒")
                .setContentText("是否进行数据处理?")
                .setCancelText("取消")
                .setConfirmText("确定")
                .showCancelButton(true)
                // 设置取消事件
                .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sDialog) {
                        // reuse previous dialog instance, keep widget user state, reset them if you need
                        sDialog.setTitleText("取消!")
                                .setContentText("已取消进行图片处理 :)")
                                .setConfirmText("OK")
                                .showCancelButton(false)
                                .setCancelClickListener(null)
                                .setConfirmClickListener(null)
                                .changeAlertType(SweetAlertDialog.ERROR_TYPE);

                        // or you can new a SweetAlertDialog to show
                               /* sDialog.dismiss();
                                new SweetAlertDialog(SampleActivity.this, SweetAlertDialog.ERROR_TYPE)
                                        .setTitleText("Cancelled!")
                                        .setContentText("Your imaginary file is safe :)")
                                        .setConfirmText("OK")
                                        .show();*/
                    }
                })
                // 设置确定事件
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sDialog) {
                        sDialog.setTitleText("已处理!")
                                .setContentText("已上传数据信息!")
                                .setConfirmText("OK")
                                .showCancelButton(false)
                                .setCancelClickListener(null)
                                .setConfirmClickListener(null)
                                .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);

                    }
                });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_process_detail);
        initView();
        initData();
        initDialog();
        setListener();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.user_info_back_btn:
                finish();
                break;
            case R.id.txt_process_detail_btn :
//                sweetAlertDialog.show();
                showDialog();
                break;
            default:
                break;
        }
    }

    /**
     * 核心处理函数，对照片上的路径直接进行图像处理
     * @param uri
     * @return
     */
    private String processPhoto(String uri){
        return null;
    }
}
